package com.acquisition.param.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dcs.cn.common.constants.Constants;
import com.dcs.cn.data.service.DayDataService;
import com.dcs.cn.data.service.HourDataService;
import com.dcs.cn.data.service.MinuteDataService;
import com.dcs.cn.data.service.RealTimeDataService;
import com.dcs.cn.data.service.SerialDataService;
import com.dcs.cn.data.service.impl.DayDataServiceImpl;
import com.dcs.cn.data.service.impl.HourDataServiceImpl;
import com.dcs.cn.data.service.impl.MinuteDataServiceImpl;
import com.dcs.cn.data.service.impl.RealTimeDataServiceImpl;
import com.dcs.cn.data.service.impl.SerialDataServiceImpl;
import com.dcs.cn.param.dao.ParamDao;
import com.dcs.cn.param.dao.impl.ParamDaoImpl;
import com.dcs.cn.param.domain.CameraParam;
import com.dcs.cn.param.domain.Hj212Param;
import com.dcs.cn.param.domain.MonitorFactorParam;
import com.dcs.cn.param.domain.ModbusSlaveParam;
import com.dcs.cn.param.domain.SingleCameraParam;
import com.dcs.cn.param.domain.SingleMonitorFactorParam;
import com.dcs.cn.param.domain.NetworkParam;
import com.dcs.cn.param.domain.Param;
import com.dcs.cn.param.domain.SerialPortNameParam;
import com.dcs.cn.param.domain.SerialPortParam;
import com.dcs.cn.param.domain.SingleSerialPortParam;
import com.dcs.cn.param.domain.UnitParam;
import com.dcs.cn.param.domain.UploadParam;
import com.dcs.cn.param.domain.WarningParam;
import com.dcs.cn.param.dto.CameraConfig;
import com.dcs.cn.param.dto.CenterServerConfig;
import com.dcs.cn.param.dto.HJ212Config;
import com.dcs.cn.param.dto.ModbusRtuConfig;
import com.dcs.cn.param.dto.ModbusSlaveConfig;
import com.dcs.cn.param.dto.SingleCameraConfig;
import com.dcs.cn.param.dto.SingleMonitorFactorConfig;
import com.dcs.cn.param.dto.NetConfig;
import com.dcs.cn.param.dto.ParamUnitConfig;
import com.dcs.cn.param.dto.SerialPortConfig;
import com.dcs.cn.param.dto.SerialPortNameConfig;
import com.dcs.cn.param.dto.SingleSerialPortConfig;
import com.dcs.cn.param.dto.WarningConfig;
import com.dcs.cn.param.service.ParamService;

/**
 * 设备参数
 * 业务逻辑层实现
 *
 * @author wuqiwei
 * @date 2020/11/20
 */
public class ParamServiceImpl implements ParamService {

	private static final Logger LOG = LoggerFactory.getLogger(ParamServiceImpl.class);

	@Override
	public int save(Param param) {
		ParamDao dao = new ParamDaoImpl();
		return dao.save(param);
	}

	@Override
	public int remove(int id) {
		ParamDao dao = new ParamDaoImpl();
		return dao.remove(id);
	}

	@Override
	public int update(Param param) {
		ParamDao dao = new ParamDaoImpl();
		return dao.update(param);
	}

	@Override
	public int update(String key, String value, int type) {
		ParamDao dao = new ParamDaoImpl();
		return dao.update(key, value, type);
	}

	@Override
	public Param get(int id) {
		ParamDao dao = new ParamDaoImpl();
		return dao.get(id);
	}

	@Override
	public Param get(String key) {
		ParamDao dao = new ParamDaoImpl();
		Map<String, Object> map = new HashMap<>(16);
		map.put("AND_EQ_key", key);
		List<Param> list = dao.queryByExample(map);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			LOG.error("未查询到该key：{} 的数据", key);
			return null;
		}
	}

	@Override
	public String getValue(String key) {
		ParamDao dao = new ParamDaoImpl();
		return dao.getValue(key);
	}

	@Override
	public List<Param> queryByExample(Map<String, Object> param) {
		ParamDao dao = new ParamDaoImpl();
		return dao.queryByExample(param);
	}

	@Override
	public List<Param> queryByExample(int type) {
		ParamDao dao = new ParamDaoImpl();
		Map<String, Object> map = new HashMap<>(16);
		map.put("AND_EQ_type", type);
		return dao.queryByExample(map);
	}

	@Override
	public int receiveAllParamOfServer(String params) {
		int count = 0;
		// 处理一下参数, 例：{"InitParam":{...}}
		JSONObject jsonObject = JSONObject.parseObject(params);
		String temp = jsonObject.getString("InitParam");
		Map<String, Object> map = JSON.parseObject(temp);
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			if (!Constants.PARAM_KEY_MN.equals(entry.getKey())) {
				count = receiveParamOfServer(entry.getValue().toString());
				if (count == 0) {
					LOG.error("参数初始化，在保存{}参数时，出现问题，其对应的要更改的参数信息：{}", entry.getKey(), entry.getValue());
					return count;
				}
			}
		}
		// 设备初始化后，清空数据库中的历史数据
		SerialDataService serialDataService = new SerialDataServiceImpl();
		DayDataService dayDataService = new DayDataServiceImpl();
		HourDataService hourDataService = new HourDataServiceImpl();
		MinuteDataService minuteDataService = new MinuteDataServiceImpl();
		RealTimeDataService realTimeDataService = new RealTimeDataServiceImpl();
		if (serialDataService.removeAll() <= 0 || dayDataService.removeAll() <= 0 || hourDataService.removeAll() <= 0 || minuteDataService.removeAll() <= 0 || realTimeDataService.removeAll() <= 0) {
			LOG.error("参数初始化，清空数据库中的历史数据，清空失败");
			count = 0;
			return count;
		} else {
			LOG.info("参数初始化，清空数据库中的历史数据，清空成功");
		}
		return count;
	}

	@Override
	public int receiveParamOfServer(String params) {
		int count = 0;
		String value;
		Map<String, Object> map = JSON.parseObject(params);
		if (map.get("type") == null) {
			LOG.error("type参数：{}，有误，为空", map.get("type"));
			return 0;
		}
		String type = Objects.requireNonNull(map.get("type")).toString();
		switch (type) {
			case Constants.DTO_TYPE_CENTER:
				UploadParam uploadParam = new UploadParam();
				transform(uploadParam, map);
				value = JSON.toJSONString(uploadParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_UPLOAD, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_HJ212:
				Hj212Param hj212Param = new Hj212Param();
				transform(hj212Param, map);
				value = JSON.toJSONString(hj212Param);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_HJ212, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_RTU:
				MonitorFactorParam monitorFactorParam = new MonitorFactorParam();
				transform(monitorFactorParam, map);
				value = JSON.toJSONString(monitorFactorParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_MONITOR, value, Constants.UPLOAD_FLAG_1);
				// 当监测因子参数修改时，清空SerialData表缓存
				SerialDataService serialDataService = new SerialDataServiceImpl();
				serialDataService.removeAll();
				break;
			case Constants.DTO_TYPE_SLAVE:
				ModbusSlaveParam modbusSlaveParam = new ModbusSlaveParam();
				transform(modbusSlaveParam, map);
				value = JSON.toJSONString(modbusSlaveParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_MODBUSSLAVE, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_NET:
				NetworkParam networkParam = new NetworkParam();
				transform(networkParam, map);
				value = JSON.toJSONString(networkParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_NETWORK, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_UNIT:
				UnitParam unitParam = new UnitParam();
				transform(unitParam, map);
				value = JSON.toJSONString(unitParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_UNIT, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_SERIAL_NAME:
				SerialPortNameParam serialPortNameParam = new SerialPortNameParam();
				transform(serialPortNameParam, map);
				value = JSON.toJSONString(serialPortNameParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_SERIALPORTNAME, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_SERIAL:
				String serialPortParamValue = getValue(Constants.PARAM_KEY_SERIALPORT);
				SerialPortParam serialPortParam;
				if (serialPortParamValue != null && !"".equals(serialPortParamValue)) {
					serialPortParam = JSON.parseObject(serialPortParamValue, SerialPortParam.class);
				} else {
					Map<String, SingleSerialPortParam> serialPortMap = new HashMap<>(16);
					serialPortParam = new SerialPortParam(serialPortMap);
				}
				if (map.get("serialPortList") != null) {
					List<Map<String, Object>> serialPortList = JSON.parseObject(JSON.toJSONString(map.get("serialPortList")), new TypeReference<List<Map<String,Object>>>(){});
					serialPortList.forEach(serialPortMap -> transform(serialPortParam, serialPortMap));
				} else {
					transform(serialPortParam, map);
				}
				value = JSON.toJSONString(serialPortParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_SERIALPORT, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_WARN:
				WarningParam warningParam = new WarningParam();
				transform(warningParam, map);
				value = JSON.toJSONString(warningParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_WARNING, value, Constants.UPLOAD_FLAG_1);
				break;
			case Constants.DTO_TYPE_CAMERA:
				CameraParam cameraParam = new CameraParam();
				transform(cameraParam, map);
				value = JSON.toJSONString(cameraParam);
				LOG.info("要修改的设备参数：{}", value);
				count = update(Constants.PARAM_KEY_CAMERA, value, Constants.UPLOAD_FLAG_1);
				break;
			default:
				LOG.error("type参数：{}，有误", type);
				break;
		}
		return count;
	}

	@Override
	public String getParamOfServer(Param param) {
		String result;
		String mn = getValue(Constants.PARAM_KEY_MN);
		switch (param.getKey()) {
			case Constants.PARAM_KEY_HJ212:
				Hj212Param hj212Param = JSON.parseObject(param.getValue(), Hj212Param.class);
				HJ212Config hj212Config = new HJ212Config();
				transform(hj212Param, hj212Config, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(hj212Config) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_MODBUSSLAVE:
				ModbusSlaveParam modbusSlaveParam = JSON.parseObject(param.getValue(), ModbusSlaveParam.class);
				ModbusSlaveConfig modbusSlaveConfig = new ModbusSlaveConfig();
				transform(modbusSlaveParam, modbusSlaveConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(modbusSlaveConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_MONITOR:
				MonitorFactorParam monitorFactorParam = JSON.parseObject(param.getValue(), MonitorFactorParam.class);
				ModbusRtuConfig modbusRtuConfig = new ModbusRtuConfig();
				transform(monitorFactorParam, modbusRtuConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(modbusRtuConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_NETWORK:
				NetworkParam networkParam = JSON.parseObject(param.getValue(), NetworkParam.class);
				NetConfig netConfig = new NetConfig();
				transform(networkParam, netConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(netConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_SERIALPORTNAME:
				SerialPortNameParam serialPortNameParam = JSON.parseObject(param.getValue(), SerialPortNameParam.class);
				SerialPortNameConfig serialPortNameConfig = new SerialPortNameConfig();
				transform(serialPortNameParam, serialPortNameConfig, mn);
				result = Constants.SEND_MSG_PARAM +JSON.toJSONString(serialPortNameConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_SERIALPORT:
				SerialPortParam serialPortParam = JSON.parseObject(param.getValue(), SerialPortParam.class);
				SerialPortConfig serialPortConfig = new SerialPortConfig();
				transform(serialPortParam, serialPortConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(serialPortConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_UNIT:
				UnitParam unitParam = JSON.parseObject(param.getValue(), UnitParam.class);
				ParamUnitConfig paramUnitConfig = new ParamUnitConfig();
				transform(unitParam, paramUnitConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(paramUnitConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_UPLOAD:
				UploadParam uploadParam = JSON.parseObject(param.getValue(), UploadParam.class);
				CenterServerConfig centerServerConfig = new CenterServerConfig();
				transform(uploadParam, centerServerConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(centerServerConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_WARNING:
				WarningParam warningParam = JSON.parseObject(param.getValue(), WarningParam.class);
				WarningConfig warningConfig = new WarningConfig();
				transform(warningParam, warningConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(warningConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			case Constants.PARAM_KEY_CAMERA:
				CameraParam cameraParam = JSON.parseObject(param.getValue(), CameraParam.class);
				CameraConfig cameraConfig = new CameraConfig();
				transform(cameraParam, cameraConfig, mn);
				result = Constants.SEND_MSG_PARAM + JSON.toJSONString(cameraConfig) + Constants.DELIMITER_SYMBOL;
				LOG.info("要上传的设备参数的报文：{}", result);
				break;
			default:
				LOG.error("param的Key参数：{}，有误，该参数无需上传", param.getKey());
				result = null;
				break;
		}
		return result;
	}

	private void transform(UploadParam uploadParam, Map<String, Object> map) {
		if (map.get("serverCount") != null) {
			uploadParam.setServerCount(Integer.parseInt(Objects.requireNonNull(map.get("serverCount")).toString()));
		} else {
			uploadParam.setServerCount(null);
		}
		if (map.get("center1Address") != null) {
			uploadParam.setIp1(Objects.requireNonNull(map.get("center1Address")).toString());
		} else {
			uploadParam.setIp1("");
		}
		if (map.get("center1Port") != null) {
			uploadParam.setPort1(Integer.parseInt(Objects.requireNonNull(map.get("center1Port")).toString()));
		} else {
			uploadParam.setPort1(null);
		}
		if (map.get("center2Address") != null) {
			uploadParam.setIp2(Objects.requireNonNull(map.get("center2Address")).toString());
		} else {
			uploadParam.setIp2("");
		}
		if (map.get("center2Port") != null) {
			uploadParam.setPort2(Integer.parseInt(Objects.requireNonNull(map.get("center2Port")).toString()));
		} else {
			uploadParam.setPort2(null);
		}
		if (map.get("center3Address") != null) {
			uploadParam.setIp3(Objects.requireNonNull(map.get("center3Address")).toString());
		} else {
			uploadParam.setIp3("");
		}
		if (map.get("center3Port") != null) {
			uploadParam.setPort3(Integer.parseInt(Objects.requireNonNull(map.get("center3Port")).toString()));
		} else {
			uploadParam.setPort3(null);
		}
		if (map.get("center4Address") != null) {
			uploadParam.setIp4(Objects.requireNonNull(map.get("center4Address")).toString());
		} else {
			uploadParam.setIp4("");
		}
		if (map.get("center4Port") != null) {
			uploadParam.setPort4(Integer.parseInt(Objects.requireNonNull(map.get("center4Port")).toString()));
		} else {
			uploadParam.setPort4(null);
		}
		if (map.get("interval") != null) {
			uploadParam.setInterval(Integer.parseInt(Objects.requireNonNull(map.get("interval")).toString()));
		} else {
			uploadParam.setInterval(null);
		}
		if (map.get("realtimeUpload") != null) {
			uploadParam.setRealTimeUpload(Integer.parseInt(Objects.requireNonNull(map.get("realtimeUpload")).toString()));
		} else {
			uploadParam.setRealTimeUpload(null);
		}
	}

	private void transform(Hj212Param hj212Param, Map<String, Object> map) {
		if (map.get("mn") != null) {
			hj212Param.setMn(Objects.requireNonNull(map.get("mn")).toString());
		} else {
			hj212Param.setMn("");
		}
		if (map.get("ST") != null) {
			hj212Param.setSt(Objects.requireNonNull(map.get("ST")).toString());
		} else {
			hj212Param.setSt("");
		}
		if (map.get("PWD") != null) {
			hj212Param.setPw(Objects.requireNonNull(map.get("PWD")).toString());
		} else {
			hj212Param.setPw("");
		}
	}

	private void transform(MonitorFactorParam monitorFactorParam, Map<String, Object> map) {
		if (map.get("interval") != null) {
			monitorFactorParam.setInterval(Integer.parseInt(Objects.requireNonNull(map.get("interval")).toString()));
		} else {
			monitorFactorParam.setInterval(null);
		}
		if (map.get("deviceType") != null) {
			monitorFactorParam.setDeviceType(Integer.parseInt(Objects.requireNonNull(map.get("deviceType")).toString()));
		} else {
			monitorFactorParam.setDeviceType(null);
		}
		if (map.get("pa") != null) {
			monitorFactorParam.setPa(Objects.requireNonNull(map.get("pa")).toString());
		} else {
			monitorFactorParam.setPa(null);
		}
		if (map.get("airK") != null) {
			monitorFactorParam.setAirK(Objects.requireNonNull(map.get("airK")).toString());
		} else {
			monitorFactorParam.setAirK(null);
		}
		if (map.get("speedK") != null) {
			monitorFactorParam.setSpeedK(Objects.requireNonNull(map.get("speedK")).toString());
		} else {
			monitorFactorParam.setSpeedK(null);
		}
		if (map.get("area") != null) {
			monitorFactorParam.setArea(Objects.requireNonNull(map.get("area")).toString());
		} else {
			monitorFactorParam.setArea(null);
		}
		if (map.get("factorConfigMap") != null) {
			Map<String, Map<String, Object>> factorConfigMap = JSON.parseObject(Objects.requireNonNull(map.get("factorConfigMap")).toString(), new TypeReference<Map<String, Map<String, Object>>>() {});
			if (factorConfigMap != null && factorConfigMap.size() > 0) {
				Map<String, SingleMonitorFactorParam> monitorFactorMap = new HashMap<>(16);
				factorConfigMap.forEach((k, v) -> monitorFactorMap.put(k, transformMonitorFactor(v)));
				monitorFactorParam.setMonitorFactorMap(monitorFactorMap);
			} else {
				monitorFactorParam.setMonitorFactorMap(null);
			}
		} else {
			monitorFactorParam.setMonitorFactorMap(null);
		}
	}

	private void transform(ModbusSlaveParam modbusSlaveParam, Map<String, Object> map) {
		if (map.get("modbusSlaveEnabled") != null) {
			modbusSlaveParam.setModbusSlaveFlag(Integer.parseInt(Objects.requireNonNull(map.get("modbusSlaveEnabled")).toString()));
		} else {
			modbusSlaveParam.setModbusSlaveFlag(null);
		}
		if (map.get("modbusAddress") != null) {
			modbusSlaveParam.setModbusAddress(Integer.parseInt(Objects.requireNonNull(map.get("modbusAddress")).toString()));
		} else {
			modbusSlaveParam.setModbusAddress(null);
		}
		if (map.get("IO1") != null) {
			modbusSlaveParam.setIo1(Integer.parseInt(Objects.requireNonNull(map.get("IO1")).toString()));
		} else {
			modbusSlaveParam.setIo1(null);
		}
		if (map.get("IO2") != null) {
			modbusSlaveParam.setIo2(Integer.parseInt(Objects.requireNonNull(map.get("IO2")).toString()));
		} else {
			modbusSlaveParam.setIo2(null);
		}
	}

	private void transform(NetworkParam networkParam, Map<String, Object> map) {
		if (map.get("APN") != null) {
			networkParam.setApnNetwork(Objects.requireNonNull(map.get("APN")).toString());
		} else {
			networkParam.setApnNetwork("");
		}
		if (map.get("usename") != null) {
			networkParam.setApnUserName(Objects.requireNonNull(map.get("usename")).toString());
		} else {
			networkParam.setApnUserName("");
		}
		if (map.get("password") != null) {
			networkParam.setApnUserPassword(Objects.requireNonNull(map.get("password")).toString());
		} else {
			networkParam.setApnUserPassword("");
		}
		if (map.get("CID") != null) {
			networkParam.setCid(Integer.parseInt(Objects.requireNonNull(map.get("CID")).toString()));
		} else {
			networkParam.setCid(null);
		}
		if (map.get("certificationType") != null) {
			networkParam.setAuthenticationType(Integer.parseInt(Objects.requireNonNull(map.get("certificationType")).toString()));
		} else {
			networkParam.setAuthenticationType(null);
		}
		if (map.get("netType") != null) {
			networkParam.setNetworkType(Integer.parseInt(Objects.requireNonNull(map.get("netType")).toString()));
		} else {
			networkParam.setNetworkType(null);
		}
		if (map.get("PIN") != null) {
			networkParam.setPin(Objects.requireNonNull(map.get("PIN")).toString());
		} else {
			networkParam.setPin("");
		}
		if (map.get("APNNumber") != null) {
			networkParam.setApnDialCenterNumber(Objects.requireNonNull(map.get("APNNumber")).toString());
		} else {
			networkParam.setApnDialCenterNumber("");
		}
	}

	private void transform(UnitParam unitParam, Map<String, Object> map) {
		if (map.get("unitMap") != null) {
			Map<String, String> unitMap = JSON.parseObject(Objects.requireNonNull(map.get("unitMap")).toString(), new TypeReference<Map<String, String>>() {});
			if (unitMap != null && unitMap.size() > 0) {
				unitParam.setUnitMap(unitMap);
			} else {
				unitParam.setUnitMap(null);
			}
		} else {
			unitParam.setUnitMap(null);
		}
	}

	private void transform(SerialPortParam serialPortParam, Map<String, Object> map) {
		SingleSerialPortParam singleSerialPortParam = new SingleSerialPortParam();
		if (map.get("serialPort") != null) {
			singleSerialPortParam.setPortCode(Objects.requireNonNull(map.get("serialPort")).toString());
		} else {
			singleSerialPortParam.setPortCode("");
		}
		if (map.get("portName") != null) {
			singleSerialPortParam.setPortNames(Objects.requireNonNull(map.get("portName")).toString());
		} else {
			singleSerialPortParam.setPortNames("");
		}
		if (map.get("baudRate") != null) {
			singleSerialPortParam.setBaudRate(Integer.parseInt(Objects.requireNonNull(map.get("baudRate")).toString()));
		} else {
			singleSerialPortParam.setBaudRate(null);
		}
		if (map.get("dataBits") != null) {
			singleSerialPortParam.setDataBits(Integer.parseInt(Objects.requireNonNull(map.get("dataBits")).toString()));
		} else {
			singleSerialPortParam.setDataBits(null);
		}
		if (map.get("checkBit") != null) {
			// 校验位None(0),Odd(1),Even(2),Mark(3),Space(4);
			switch (Objects.requireNonNull(map.get("checkBit")).toString()) {
				case "None":
					singleSerialPortParam.setParity(0);
					break;
				case "Odd":
					singleSerialPortParam.setParity(1);
					break;
				case "Even":
					singleSerialPortParam.setParity(2);
					break;
				case "Mark":
					singleSerialPortParam.setParity(3);
					break;
				case "Space":
					singleSerialPortParam.setParity(4);
					break;
				default:
					LOG.error("校验位None(0),Odd(1),Even(2),Mark(3),Space(4)，没有该类型：{}", map.get("checkBit"));
					break;
			}
		} else {
			singleSerialPortParam.setParity(null);
		}
		if (map.get("stopBit") != null) {
			singleSerialPortParam.setStopBits(Integer.parseInt(Objects.requireNonNull(map.get("stopBit")).toString()));
		} else {
			singleSerialPortParam.setStopBits(null);
		}
		serialPortParam.getSerialPortMap().put(Objects.requireNonNull(map.get("serialPort")).toString(), singleSerialPortParam);
	}

	private void transform(SerialPortNameParam serialPortNameParam, Map<String, Object> map) {
		if (map.get("SerialPortNames") != null) {
			serialPortNameParam.setSerialPortNames(Objects.requireNonNull(map.get("SerialPortNames")).toString());
		} else {
			serialPortNameParam.setSerialPortNames("");
		}
	}

	private void transform(WarningParam warningParam, Map<String, Object> map) {
		if (map.get("alarmMap") != null) {
			Map<String, JSONObject> warningMap = JSON.parseObject(Objects.requireNonNull(map.get("alarmMap")).toString(), new TypeReference<Map<String, JSONObject>>() {});
			if (warningMap != null && warningMap.size() > 0) {
				warningParam.setWarningMap(warningMap);
			} else {
				warningParam.setWarningMap(null);
			}
		} else {
			warningParam.setWarningMap(null);
		}
	}

	private void transform(CameraParam cameraParam, Map<String, Object> map) {
		if (map.get("count") != null) {
			cameraParam.setCount(Integer.parseInt(Objects.requireNonNull(map.get("count")).toString()));
		} else {
			cameraParam.setCount(null);
		}
		if (map.get("cameraConfigMap") != null) {
			Map<String, Map<String, Object>> factorConfigMap = JSON.parseObject(Objects.requireNonNull(map.get("cameraConfigMap")).toString(), new TypeReference<Map<String, Map<String, Object>>>() {});
			if (factorConfigMap != null && factorConfigMap.size() > 0) {
				Map<String, SingleCameraParam> cameraParamMap = new HashMap<>(16);
				factorConfigMap.forEach((k, v) -> cameraParamMap.put(k, transformCamera(v)));
				cameraParam.setCameraParamMap(cameraParamMap);
			} else {
				cameraParam.setCameraParamMap(null);
			}
		} else {
			cameraParam.setCameraParamMap(null);
		}
	}

	private void transform(Hj212Param hj212Param, HJ212Config hj212Config, String mn) {
		hj212Config.setType(Constants.DTO_TYPE_HJ212);
		hj212Config.setMn(mn);
		hj212Config.setPWD(hj212Param.getPw());
		hj212Config.setST(hj212Param.getSt());
	}

	private void transform(MonitorFactorParam monitorFactorParam, ModbusRtuConfig modbusRtuConfig, String mn) {
		modbusRtuConfig.setType(Constants.DTO_TYPE_RTU);
		modbusRtuConfig.setMn(mn);
		modbusRtuConfig.setInterval(monitorFactorParam.getInterval());
		modbusRtuConfig.setDeviceType(monitorFactorParam.getDeviceType());
		modbusRtuConfig.setPa(monitorFactorParam.getPa());
		modbusRtuConfig.setSpeedK(monitorFactorParam.getSpeedK());
		modbusRtuConfig.setAirK(monitorFactorParam.getAirK());
		modbusRtuConfig.setArea(monitorFactorParam.getArea());
		Map<String, SingleMonitorFactorConfig> factorConfigMap = new HashMap<>(16);
		monitorFactorParam.getMonitorFactorMap().forEach((k, v) -> factorConfigMap.put(k, transformSingleMonitorFactorConfig(v)));
		modbusRtuConfig.setFactorConfigMap(factorConfigMap);
	}

	private void transform(ModbusSlaveParam modbusSlaveParam, ModbusSlaveConfig modbusSlaveConfig, String mn) {
		modbusSlaveConfig.setType(Constants.DTO_TYPE_SLAVE);
		modbusSlaveConfig.setMn(mn);
		modbusSlaveConfig.setIO1(modbusSlaveParam.getIo1());
		modbusSlaveConfig.setIO2(modbusSlaveParam.getIo2());
		modbusSlaveConfig.setModbusAddress(modbusSlaveParam.getModbusAddress());
		modbusSlaveConfig.setModbusSlaveEnabled(modbusSlaveParam.getModbusSlaveFlag());
	}

	private void transform(NetworkParam networkParam, NetConfig netConfig, String mn) {
		netConfig.setType(Constants.DTO_TYPE_NET);
		netConfig.setMn(mn);
		netConfig.setAPN(networkParam.getApnNetwork());
		netConfig.setAPNNumber(networkParam.getApnDialCenterNumber());
		netConfig.setCertificationType(networkParam.getAuthenticationType());
		netConfig.setCID(networkParam.getCid());
		netConfig.setNetType(networkParam.getNetworkType());
		netConfig.setPassword(networkParam.getApnUserPassword());
		netConfig.setPIN(networkParam.getPin());
		netConfig.setUsename(networkParam.getApnUserName());
	}

	private void transform(SerialPortNameParam serialPortNameParam, SerialPortNameConfig serialPortNameConfig, String mn) {
		serialPortNameConfig.setType(Constants.DTO_TYPE_SERIAL_NAME);
		serialPortNameConfig.setMn(mn);
		serialPortNameConfig.setSerialPortNames(serialPortNameParam.getSerialPortNames());
	}

	private void transform(SerialPortParam serialPortParam, SerialPortConfig serialPortConfig, String mn) {
		serialPortConfig.setType(Constants.DTO_TYPE_SERIAL);
		serialPortConfig.setMn(mn);
		Map<String, SingleSerialPortConfig> serialPortMaps = new HashMap<>(16);
		Map<String, SingleSerialPortParam> serialPortMap = serialPortParam.getSerialPortMap();
		for (Map.Entry<String, SingleSerialPortParam> entry : serialPortMap.entrySet()) {
			serialPortMaps.put(entry.getValue().getPortNames(), transformSingleSerialPortConfig(entry.getValue()));
		}
		serialPortConfig.setSerialPortMap(serialPortMaps);
	}

	private void transform(UnitParam unitParam, ParamUnitConfig paramUnitConfig, String mn) {
		paramUnitConfig.setType(Constants.DTO_TYPE_UNIT);
		paramUnitConfig.setMn(mn);
		paramUnitConfig.setUnitMap(unitParam.getUnitMap());
	}

	private void transform(UploadParam uploadParam, CenterServerConfig centerServerConfig, String mn) {
		centerServerConfig.setType(Constants.DTO_TYPE_CENTER);
		centerServerConfig.setMn(mn);
		centerServerConfig.setServerCount(uploadParam.getServerCount());
		centerServerConfig.setCenter1Address(uploadParam.getIp1());
		centerServerConfig.setCenter1Port(uploadParam.getPort1());
		centerServerConfig.setCenter2Address(uploadParam.getIp2());
		centerServerConfig.setCenter2Port(uploadParam.getPort2());
		centerServerConfig.setCenter3Address(uploadParam.getIp3());
		centerServerConfig.setCenter3Port(uploadParam.getPort3());
		centerServerConfig.setCenter4Address(uploadParam.getIp4());
		centerServerConfig.setCenter4Port(uploadParam.getPort4());
		centerServerConfig.setRealtimeUpload(uploadParam.getRealTimeUpload());
		centerServerConfig.setInterval(uploadParam.getInterval());
	}

	private void transform(WarningParam warningParam, WarningConfig warningConfig, String mn) {
		warningConfig.setType(Constants.DTO_TYPE_WARN);
		warningConfig.setMn(mn);
		warningConfig.setAlarmMap(warningParam.getWarningMap());
	}

	private void transform(CameraParam cameraParam, CameraConfig cameraConfig, String mn) {
		cameraConfig.setType(Constants.DTO_TYPE_CAMERA);
		cameraConfig.setMn(mn);
		cameraConfig.setCount(cameraParam.getCount());
		Map<String, SingleCameraConfig> cameraConfigMap = new HashMap<>(16);
		cameraParam.getCameraParamMap().forEach((k, v) -> cameraConfigMap.put(k, transformSingleCameraConfig(v)));
		cameraConfig.setCameraConfigMap(cameraConfigMap);
	}

	private SingleMonitorFactorParam transformMonitorFactor(Map<String, Object> map) {
		SingleMonitorFactorParam monitorFactorParam = new SingleMonitorFactorParam();
		if (map.get("monitorFactor") != null) {
			monitorFactorParam.setMonitorFactorKey(Objects.requireNonNull(map.get("monitorFactor")).toString());
		} else {
			monitorFactorParam.setMonitorFactorKey("");
		}
		if (map.get("factorName") != null) {
			monitorFactorParam.setFactorName(Objects.requireNonNull(map.get("factorName")).toString());
		} else {
			monitorFactorParam.setFactorName("");
		}
		if (map.get("serialPort") != null) {
			monitorFactorParam.setPortCode(Objects.requireNonNull(map.get("serialPort")).toString());
		} else {
			monitorFactorParam.setPortCode("");
		}
		if (map.get("protocolType") != null) {
			monitorFactorParam.setProtocolType(Integer.parseInt(Objects.requireNonNull(map.get("protocolType")).toString()));
		} else {
			monitorFactorParam.setProtocolType(null);
		}
		if (map.get("slaveDeviceAddress") != null) {
			monitorFactorParam.setSlaveId(Integer.parseInt(Objects.requireNonNull(map.get("slaveDeviceAddress")).toString()));
		} else {
			monitorFactorParam.setSlaveId(null);
		}
		if (map.get("register") != null) {
			monitorFactorParam.setRegisterAddress(Integer.parseInt(Objects.requireNonNull(map.get("register")).toString()));
		} else {
			monitorFactorParam.setRegisterAddress(null);
		}
		if (map.get("dataType") != null) {
			monitorFactorParam.setDataType(Integer.parseInt(Objects.requireNonNull(map.get("dataType")).toString()));
		} else {
			monitorFactorParam.setDataType(null);
		}
		if (map.get("radixPointSize") != null) {
			monitorFactorParam.setDecimalNum(Integer.parseInt(Objects.requireNonNull(map.get("radixPointSize")).toString()));
		} else {
			monitorFactorParam.setDecimalNum(null);
		}
		return monitorFactorParam;
	}

	private SingleCameraParam transformCamera(Map<String, Object> map) {
		SingleCameraParam singleCameraParam = new SingleCameraParam();
		if (map.get("ip") != null) {
			singleCameraParam.setIp(Objects.requireNonNull(map.get("ip")).toString());
		} else {
			singleCameraParam.setIp(null);
		}
		if (map.get("port") != null) {
			singleCameraParam.setPort(Integer.parseInt(Objects.requireNonNull(map.get("port")).toString()));
		} else {
			singleCameraParam.setPort(null);
		}
		if (map.get("username") != null) {
			singleCameraParam.setUsername(Objects.requireNonNull(map.get("username")).toString());
		} else {
			singleCameraParam.setUsername(null);
		}
		if (map.get("password") != null) {
			singleCameraParam.setPassword(Objects.requireNonNull(map.get("password")).toString());
		} else {
			singleCameraParam.setPassword(null);
		}
		if (map.get("type") != null) {
			singleCameraParam.setType(Integer.parseInt(Objects.requireNonNull(map.get("type")).toString()));
		} else {
			singleCameraParam.setType(null);
		}
		return singleCameraParam;
	}

	private SingleMonitorFactorConfig transformSingleMonitorFactorConfig(SingleMonitorFactorParam singleMonitorFactorParam) {
		SingleMonitorFactorConfig singleMonitorFactorConfig = new SingleMonitorFactorConfig();
		singleMonitorFactorConfig.setDataType(singleMonitorFactorParam.getDataType());
		singleMonitorFactorConfig.setFactorName(singleMonitorFactorParam.getFactorName());
		singleMonitorFactorConfig.setMonitorFactor(singleMonitorFactorParam.getMonitorFactorKey());
		singleMonitorFactorConfig.setProtocolType(singleMonitorFactorParam.getProtocolType());
		singleMonitorFactorConfig.setRadixPointSize(singleMonitorFactorParam.getDecimalNum());
		singleMonitorFactorConfig.setRegister(singleMonitorFactorParam.getRegisterAddress());
		singleMonitorFactorConfig.setSerialPort(singleMonitorFactorParam.getPortCode());
		singleMonitorFactorConfig.setSlaveDeviceAddress(singleMonitorFactorParam.getSlaveId());
		return singleMonitorFactorConfig;
	}

	private SingleSerialPortConfig transformSingleSerialPortConfig(SingleSerialPortParam singleSerialPortParam) {
		SingleSerialPortConfig singleSerialPortConfig = new SingleSerialPortConfig();
		singleSerialPortConfig.setBaudRate(singleSerialPortParam.getBaudRate());
		switch (singleSerialPortParam.getParity()) {
			case 0:
				singleSerialPortConfig.setCheckBit("None");
				break;
			case 1:
				singleSerialPortConfig.setCheckBit("Odd");
				break;
			case 2:
				singleSerialPortConfig.setCheckBit("Even");
				break;
			case 3:
				singleSerialPortConfig.setCheckBit("Mark");
				break;
			case 4:
				singleSerialPortConfig.setCheckBit("Space");
				break;
			default:
				LOG.error("校验位None(0),Odd(1),Even(2),Mark(3),Space(4)，没有该类型：{}", singleSerialPortParam.getParity());
				break;
		}
		singleSerialPortConfig.setDataBits(singleSerialPortParam.getDataBits());
		singleSerialPortConfig.setPortName(singleSerialPortParam.getPortNames());
		singleSerialPortConfig.setSerialPort(singleSerialPortParam.getPortCode());
		singleSerialPortConfig.setStopBit(singleSerialPortParam.getStopBits());
		return singleSerialPortConfig;
	}

	private SingleCameraConfig transformSingleCameraConfig(SingleCameraParam singleCameraParam) {
		SingleCameraConfig singleCameraConfig = new SingleCameraConfig();
		singleCameraConfig.setIp(singleCameraParam.getIp());
		singleCameraConfig.setPort(singleCameraParam.getPort());
		singleCameraConfig.setUsername(singleCameraParam.getUsername());
		singleCameraConfig.setPassword(singleCameraParam.getPassword());
		singleCameraConfig.setType(singleCameraParam.getType());
		return singleCameraConfig;
	}

}
